home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / sun / jpeg / lib / djpeg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-15  |  13.2 KB  |  453 lines

  1. /*
  2.  * jdmain.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains a command-line user interface for the JPEG decompressor.
  9.  * It should work on any system with Unix- or MS-DOS-style command lines.
  10.  *
  11.  * Two different command line styles are permitted, depending on the
  12.  * compile-time switch TWO_FILE_COMMANDLINE:
  13.  *    djpeg [options]  inputfile outputfile
  14.  *    djpeg [options]  [inputfile]
  15.  * In the second style, output is always to standard output, which you'd
  16.  * normally redirect to a file or pipe to some other program.  Input is
  17.  * either from a named file or from standard input (typically redirected).
  18.  * The second style is convenient on Unix but is unhelpful on systems that
  19.  * don't support pipes.  Also, you MUST use the first style if your system
  20.  * doesn't do binary I/O to stdin/stdout.
  21. %
  22. % Modified:     Jin Guojun - LBL, Image Technology Group
  23. %       Date:   April 14, 1992
  24. %       Goal:   To be easily handled by conversion library - CCS (c)
  25. %        HIPS, FITS, GIF, RLE, SUN-raster, PNM, TIFF, PICT ...
  26. %        can be compressed by cjpeg now, directly displayed by tuner,
  27. %        decompressed to other image type by torle, torast, and color_ps.
  28. %        These type of images can be determined by program `headers'.
  29.  */
  30.  
  31. #include "jinclude.h"
  32. #ifdef INCLUDES_ARE_ANSI
  33. #include <stdlib.h>        /* to declare exit() */
  34. #endif
  35. #include <ctype.h>        /* to declare isupper(), tolower() */
  36. #ifdef NEED_SIGNAL_CATCHER
  37. #include <signal.h>        /* to declare signal() */
  38. #endif
  39. #ifdef USE_SETMODE
  40. #include <fcntl.h>        /* to declare setmode() */
  41. #endif
  42.  
  43. #ifdef THINK_C
  44. #include <console.h>        /* command-line reader for Macintosh */
  45. #endif
  46.  
  47. #ifdef DONT_USE_B_MODE        /* define mode parameters for fopen() */
  48. #define READ_BINARY    "r"
  49. #define WRITE_BINARY    "w"
  50. #else
  51. #define READ_BINARY    "rb"
  52. #define WRITE_BINARY    "wb"
  53. #endif
  54.  
  55. #ifndef EXIT_FAILURE        /* define exit() codes if not provided */
  56. #define EXIT_FAILURE  1
  57. #endif
  58. #ifndef EXIT_SUCCESS
  59. #ifdef VMS
  60. #define EXIT_SUCCESS  1        /* VMS is very nonstandard */
  61. #else
  62. #define EXIT_SUCCESS  0
  63. #endif
  64. #endif
  65.  
  66.  
  67. #include "jversion.h"        /* for version message */
  68.  
  69.  
  70. #ifndef DEFAULT_FMT        /* so can override from CFLAGS in Makefile */
  71. #define DEFAULT_FMT    FMT_PPM
  72. #endif
  73.  
  74. extern IMAGE_FORMATS requested_fmt;
  75.  
  76. /*
  77.  * Signal catcher to ensure that temporary files are removed before aborting.
  78.  * NB: for Amiga Manx C this is actually a global routine named _abort();
  79.  * see -Dsignal_catcher=_abort in CFLAGS.  Talk about bogus...
  80.  */
  81.  
  82. #ifdef NEED_SIGNAL_CATCHER
  83.  
  84. static external_methods_ptr emethods; /* for access to free_all */
  85.  
  86. GLOBAL void
  87. signal_catcher (int signum)
  88. {
  89.   if (emethods != NULL) {
  90.     emethods->trace_level = 0;    /* turn off trace output */
  91.     (*emethods->free_all) ();    /* clean up memory allocation & temp files */
  92.   }
  93.   exit(EXIT_FAILURE);
  94. }
  95.  
  96. #endif
  97.  
  98.  
  99. /*
  100.  * Optional routine to display a percent-done figure on stderr.
  101.  * See jddeflts.c for explanation of the information used.
  102.  */
  103.  
  104. #ifdef PROGRESS_REPORT
  105.  
  106. METHODDEF void
  107. progress_monitor (decompress_info_ptr cinfo, long loopcounter, long looplimit)
  108. {
  109.   if (cinfo->total_passes > 1) {
  110.     fprintf(stderr, "\rPass %d/%d: %3d%% ",
  111.         cinfo->completed_passes+1, cinfo->total_passes,
  112.         (int) (loopcounter*100L/looplimit));
  113.   } else {
  114.     fprintf(stderr, "\r %3d%% ",
  115.         (int) (loopcounter*100L/looplimit));
  116.   }
  117.   fflush(stderr);
  118. }
  119.  
  120. #endif
  121.  
  122.  
  123. /*
  124.  * Argument-parsing code.
  125.  * The switch parser is designed to be useful with DOS-style command line
  126.  * syntax, ie, intermixed switches and file names, where only the switches
  127.  * to the left of a given file name affect processing of that file.
  128.  * The main program in this file doesn't actually use this capability...
  129.  */
  130.  
  131.  
  132. static char * progname;        /* program name for error messages */
  133.  
  134. LOCAL void
  135. usage (void)
  136. /* complain about bad command line */
  137. {
  138.   fprintf(stderr, "usage: %s [switches] ", progname);
  139. #ifdef TWO_FILE_COMMANDLINE
  140.   fprintf(stderr, "inputfile outputfile\n");
  141. #else
  142.   fprintf(stderr, "[inputfile]\n");
  143. #endif
  144.  
  145.   fprintf(stderr, "Switches (names may be abbreviated):\n");
  146.   fprintf(stderr, "  -colors N      Reduce image to no more than N colors\n");
  147. #ifdef GIF_SUPPORTED
  148.   fprintf(stderr, "  -gif           Select GIF output format\n");
  149. #endif
  150. #ifdef PPM_SUPPORTED
  151.   fprintf(stderr, "  -pnm           Select PBMPLUS (PPM/PGM) output format (default)\n");
  152. #endif
  153.   fprintf(stderr, "  -quantize N    Same as -colors N\n");
  154. #ifdef    HIPS_IMAGE
  155.   fprintf(stderr, "  -hips          Select HIPS output format\n");
  156. #endif
  157. #ifdef RLE_SUPPORTED
  158.   fprintf(stderr, "  -rle           Select Utah RLE output format\n");
  159. #endif
  160. #ifdef TARGA_SUPPORTED
  161.   fprintf(stderr, "  -targa         Select Targa output format\n");
  162. #endif
  163.   fprintf(stderr, "Switches for advanced users:\n");
  164. #ifdef BLOCK_SMOOTHING_SUPPORTED
  165.   fprintf(stderr, "  -blocksmooth   Apply cross-block smoothing\n");
  166. #endif
  167.   fprintf(stderr, "  -grayscale     Force grayscale output\n");
  168.   fprintf(stderr, "  -nodither      Don't use dithering in quantization\n");
  169. #ifdef QUANT_1PASS_SUPPORTED
  170.   fprintf(stderr, "  -onepass       Use 1-pass quantization (fast, low quality)\n");
  171. #endif
  172.   fprintf(stderr, "  -maxmemory N   Maximum memory to use (in kbytes)\n");
  173.   fprintf(stderr, "  -verbose  or  -debug   Emit debug output\n");
  174.   exit(EXIT_FAILURE);
  175. }
  176.  
  177.  
  178. LOCAL boolean
  179. keymatch (char * arg, const char * keyword, int minchars)
  180. /* Case-insensitive matching of (possibly abbreviated) keyword switches. */
  181. /* keyword is the constant keyword (must be lower case already), */
  182. /* minchars is length of minimum legal abbreviation. */
  183. {
  184.   register int ca, ck;
  185.   register int nmatched = 0;
  186.  
  187.   while ((ca = *arg++) != '\0') {
  188.     if ((ck = *keyword++) == '\0')
  189.       return FALSE;        /* arg longer than keyword, no good */
  190.     if (isupper(ca))        /* force arg to lcase (assume ck is already) */
  191.       ca = tolower(ca);
  192.     if (ca != ck)
  193.       return FALSE;        /* no good */
  194.     nmatched++;            /* count matched characters */
  195.   }
  196.   /* reached end of argument; fail if it's too short for unique abbrev */
  197.   if (nmatched < minchars)
  198.     return FALSE;
  199.   return TRUE;            /* A-OK */
  200. }
  201.  
  202.  
  203. LOCAL int
  204. parse_switches (decompress_info_ptr cinfo, int last_file_arg_seen,
  205.         int argc, char **argv)
  206. /* Initialize cinfo with default switch settings, then parse option switches.
  207.  * Returns argv[] index of first file-name argument (== argc if none).
  208.  * Any file names with indexes <= last_file_arg_seen are ignored;
  209.  * they have presumably been processed in a previous iteration.
  210.  * (Pass 0 for last_file_arg_seen on the first or only iteration.)
  211.  */
  212. {
  213.   int argn;
  214.   char * arg;
  215.  
  216.   /* (Re-)initialize the system-dependent error and memory managers. */
  217.   jselerror(cinfo->emethods);    /* error/trace message routines */
  218.   jselmemmgr(cinfo->emethods);    /* memory allocation routines */
  219. /*  cinfo->methods->d_ui_method_selection = d_ui_method_selection;    */
  220.  
  221.   /* Now OK to enable signal catcher. */
  222. #ifdef NEED_SIGNAL_CATCHER
  223.   emethods = cinfo->emethods;
  224. #endif
  225.  
  226.   /* Set up default JPEG parameters. */
  227.   j_d_defaults(cinfo, TRUE);
  228.   requested_fmt = DEFAULT_FMT;    /* set default output file format */
  229.  
  230.   /* Scan command line options, adjust parameters */
  231.  
  232.   for (argn = 1; argn < argc; argn++) {
  233.     arg = argv[argn];
  234.     if (*arg != '-') {
  235.       /* Not a switch, must be a file name argument */
  236.       if (argn <= last_file_arg_seen)
  237.     continue;        /* ignore it if previously processed */
  238.       break;            /* else done parsing switches */
  239.     }
  240.     arg++;            /* advance past switch marker character */
  241.  
  242.     if (keymatch(arg, "blocksmooth", 1)) {
  243.       /* Enable cross-block smoothing. */
  244.       cinfo->do_block_smoothing = TRUE;
  245.  
  246.     } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||
  247.            keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) {
  248.       /* Do color quantization. */
  249.       int val;
  250.  
  251.       if (++argn >= argc)    /* advance to next argument */
  252.     usage();
  253.       if (sscanf(argv[argn], "%d", &val) != 1)
  254.     usage();
  255.       cinfo->desired_number_of_colors = val;
  256.       cinfo->quantize_colors = TRUE;
  257.  
  258.     } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
  259.       /* Enable debug printouts. */
  260.       /* On first -d, print version identification */
  261.       if (last_file_arg_seen == 0 && cinfo->emethods->trace_level == 0)
  262.     fprintf(stderr, "Independent JPEG Group's DJPEG, version %s\n%s\n",
  263.         JVERSION, JCOPYRIGHT);
  264.       cinfo->emethods->trace_level++;
  265.  
  266.     } else if (keymatch(arg, "gif", 1)) {
  267.       /* GIF output format. */
  268.       requested_fmt = FMT_GIF;
  269.  
  270.     } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
  271.       /* Force monochrome output. */
  272.       cinfo->out_color_space = CS_GRAYSCALE;
  273.  
  274.     } else if (keymatch(arg, "maxmemory", 1)) {
  275.       /* Maximum memory in Kb (or Mb with 'm'). */
  276.       long lval;
  277.       char ch = 'x';
  278.  
  279.       if (++argn >= argc)    /* advance to next argument */
  280.     usage();
  281.       if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
  282.     usage();
  283.       if (ch == 'm' || ch == 'M')
  284.     lval *= 1000L;
  285.       cinfo->emethods->max_memory_to_use = lval * 1000L;
  286.  
  287.     } else if (keymatch(arg, "nodither", 3)) {
  288.       /* Suppress dithering in color quantization. */
  289.       cinfo->use_dithering = FALSE;
  290.  
  291.     } else if (keymatch(arg, "onepass", 1)) {
  292.       /* Use fast one-pass quantization. */
  293.       cinfo->two_pass_quantize = FALSE;
  294.  
  295.     } else if (keymatch(arg, "pnm", 1)) {
  296.       /* PPM/PGM output format. */
  297.       requested_fmt = FMT_PPM;
  298.  
  299.     } else if (keymatch(arg, "rle", 1)) {
  300.       /* RLE output format. */
  301.       requested_fmt = FMT_RLE;
  302.  
  303.     } else if (keymatch(arg, "targa", 1)) {
  304.       /* Targa output format. */
  305.       requested_fmt = FMT_TARGA;
  306.  
  307. #ifdef    HIPS_IMAGE
  308.     } else if (keymatch(arg, "hips", 1)) {
  309.     /* HIPS output format. */
  310.     format_init(&uimg, IMAGE_INIT_TYPE, HIPS, uimg.color_dpy=-1, "djpeg", "D2-2");
  311.     requested_fmt = FMT_DEFAULT;
  312. #endif
  313.  
  314.     } else {
  315.       usage();            /* bogus switch */
  316.     }
  317.   }
  318.  
  319.   return argn;            /* return index of next arg (file name) */
  320. }
  321.  
  322.  
  323. /*
  324.  * The main program.
  325.  */
  326.  
  327. U_IMAGE    uimg;
  328. extern    struct Decompress_methods_struct    dc_methods;
  329.  
  330. GLOBAL int
  331. main (int argc, char **argv)
  332. {
  333.   int file_index;
  334.  
  335.   /* On Mac, fetch a command line. */
  336. #ifdef THINK_C
  337.   argc = ccommand(&argv);
  338. #endif
  339.  
  340.   progname = argv[0];
  341.  
  342.   /* Set up links to method structures. */
  343.   dinfo.methods = &dc_methods;
  344.   dinfo.emethods = &e_methods;
  345.  
  346.   /* Install, but don't yet enable signal catcher. */
  347. #ifdef NEED_SIGNAL_CATCHER
  348.   emethods = NULL;
  349.   signal(SIGINT, signal_catcher);
  350. #ifdef SIGTERM            /* not all systems have SIGTERM */
  351.   signal(SIGTERM, signal_catcher);
  352. #endif
  353. #endif
  354.  
  355.   /* Scan command line: set up compression parameters, input & output files. */
  356.  
  357.   file_index = parse_switches(&dinfo, 0, argc, argv);
  358.  
  359. #ifdef TWO_FILE_COMMANDLINE
  360.  
  361.   if (file_index != argc-2) {
  362.     fprintf(stderr, "%s: must name one input and one output file\n", progname);
  363.     usage();
  364.   }
  365.   if ((dinfo.input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
  366.     fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
  367.     exit(EXIT_FAILURE);
  368.   }
  369.   if ((dinfo.output_file = fopen(argv[file_index+1], WRITE_BINARY)) == NULL) {
  370.     fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index+1]);
  371.     exit(EXIT_FAILURE);
  372.   }
  373.  
  374. #else /* not TWO_FILE_COMMANDLINE -- use Unix style */
  375.  
  376.   dinfo.input_file = stdin;    /* default input file */
  377.   dinfo.output_file = stdout;    /* always the output file */
  378.  
  379. #ifdef USE_SETMODE        /* need to hack file mode? */
  380.   setmode(fileno(stdin), O_BINARY);
  381.   setmode(fileno(stdout), O_BINARY);
  382. #endif
  383.  
  384.   if (file_index < argc-1) {
  385.     fprintf(stderr, "%s: only one input file\n", progname);
  386.     usage();
  387.   }
  388.   if (file_index < argc) {
  389.     if ((dinfo.input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
  390.       fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
  391.       exit(EXIT_FAILURE);
  392.     }
  393.   }
  394.  
  395. #endif /* TWO_FILE_COMMANDLINE */
  396.   
  397.   /* Set up to read a JFIF or baseline-JPEG file. */
  398.   /* A smarter UI would inspect the first few bytes of the input file */
  399.   /* to determine its type. */
  400. #ifdef JFIF_SUPPORTED
  401.   jselrjfif(&dinfo);
  402. #else
  403.   You shoulda defined JFIF_SUPPORTED.   /* deliberate syntax error */
  404. #endif
  405.  
  406. #ifdef PROGRESS_REPORT
  407.   /* Start up progress display, unless trace output is on */
  408.   if (e_methods.trace_level == 0)
  409.     dc_methods.progress_monitor = progress_monitor;
  410. #endif
  411.  
  412.     uimg.IN_FP = dinfo.input_file;
  413.     uimg.OUT_FP = dinfo.output_file;
  414.     io_test(fileno(in_fp), usage(argv[0]));
  415.  
  416. #ifdef    STREAM_IMAGE
  417.     format_init(&uimg, IMAGE_INIT_TYPE, HIPS, uimg.color_dpy=-1, "djpeg", "D2-2");
  418.     if (!(*uimg.header_handle)(HEADER_READ, &uimg, 0, 0, 0))    {
  419. #else
  420.     if (!jpeg_header_handle(HEADER_READ, &uimg, 0, 0, 0))    {
  421. #endif
  422. #ifdef    HIPS_IMAGE
  423.         if (requested_fmt == FMT_DEFAULT && uimg.o_type == HIPS)    {
  424.         (*uimg.header_handle)(HEADER_WRITE, &uimg, argc, argv, 1);
  425.         if (!uimg.src)
  426.             uimg.src = nzalloc(uimg.width*uimg.height,
  427.                 uimg.dpy_channels, "jpsrc");
  428.         }
  429. #endif
  430. #ifdef    STREAM_IMAGE
  431.         if (uimg.in_type != JPEG)    {
  432.         do    {
  433.             (*uimg.std_swif)(FI_LOAD_FILE,&uimg, 0, 0, 0);
  434.             fwrite(hhd.image, hhd.cols, hhd.rows, out_fp);
  435.         } while (++uimg.fn < uimg.frames);
  436.         } else
  437. #endif
  438.         jpeg_decompress(&dinfo);
  439.     }
  440.  
  441. #ifdef PROGRESS_REPORT
  442.   /* Clear away progress display */
  443.   if (e_methods.trace_level == 0) {
  444.     fprintf(stderr, "\r                \r");
  445.     fflush(stderr);
  446.   }
  447. #endif
  448.  
  449.   /* All done. */
  450.   exit(EXIT_SUCCESS);
  451.   return 0;            /* suppress no-return-value warnings */
  452. }
  453.